from PyQt6.QtWidgets import QWidget, QDialog, QMessageBox, QComboBox, QPushButton, QVBoxLayout, QApplication
from PyQt6.QtCore import Qt, QPoint
from again.Data_Management_page.TypeSetting import Ui_TypeSetting
import picture.resources_rc
class TypeSettingWindow(QDialog, Ui_TypeSetting):
    def __init__(self, column_index, header_text, table_widget, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        # 更新标签文本


        self.setWindowTitle("TypeSetting")
        self.column_index = column_index
        self.table_widget = table_widget
        self.header_text = header_text
        self.update_label()

        self.setWindowFlags(Qt.WindowType.FramelessWindowHint)

        self.setMouseTracking(True)
        self.is_maximized = False
        self.initDrag()

        self.initial_geometry = self.geometry()

        self.pBtnClose.clicked.connect(self.close)
        self.pBtnSave.clicked.connect(self.save_settings)
        self.pBtnCancel.clicked.connect(self.close)

        self.comboBox.setCurrentIndex(0)  # 默认选中第一个选项

        self.selected_type = None  # 用于存储用户选择的类型

        # 设置弹窗的初始位置
        self.set_initial_position()

    def update_label(self):
        # 设置标签文本为包含 column_index 的动态内容
        self.label.setText(f"{self.header_text} 的属性设置：")
    def set_initial_position(self):
        if self.parent():
            # 获取父窗口的位置
            parent_pos = self.parent().mapToGlobal(QPoint(0, 0))
            parent_size = self.parent().size()
            # 设置弹窗的位置（例如：父窗口的右下角）
            x = int((parent_pos.x() + parent_size.width() - self.size().width())/2)
            y = int((parent_pos.y() + parent_size.height() - self.size().height())/2)
            self.move(x, y)

    def initDrag(self):
        self.moveDrag = False

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.MouseButton.LeftButton:
            if not self.moveDrag:
                self.moveDragPosition = event.globalPosition().toPoint() - self.pos()
                self.moveDrag = True
            self.move(event.globalPosition().toPoint() - self.moveDragPosition)
            event.accept()
        else:
            self.moveDrag = False

    def mouseReleaseEvent(self, event):
        self.moveDrag = False

    def save_settings(self):
        data_type = self.comboBox.currentText()

        if data_type == "多类（二类）枚举类型":
            self.handle_categorical()
            self.selected_type = "object"  # 使用字符串表示类型
        elif data_type == "实数（整数）类型":
            self.handle_numerical()
            self.selected_type = "float"  # 使用字符串表示类型
        self.accept()  # 保存设置并关闭对话框

    def handle_categorical(self):
        QMessageBox.information(self, "类型设置", f"列 {self.column_index} 设置为多类（二类）枚举类型")
        self.table_widget.setHorizontalHeaderLabels(self.update_header_labels("Categorical"))

    def handle_numerical(self):
        QMessageBox.information(self, "类型设置", f"列 {self.column_index} 设置为实数（整数）类型")
        self.table_widget.setHorizontalHeaderLabels(self.update_header_labels("Numerical"))

    def update_header_labels(self, new_type):
        headers = [self.table_widget.horizontalHeaderItem(i).text() for i in range(self.table_widget.columnCount())]

        # 去除已有的类型标签
        original_label = headers[self.column_index]
        if "(" in original_label:
            original_label = original_label.split(" (")[0]

        # 更新标签为新类型
        headers[self.column_index] = f"{original_label} ({new_type})"

        return headers

    def get_selected_type(self):
        # 返回用户选择的类型
        print(self.selected_type)
        return self.selected_type
